/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.test.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.test.entity.MmDad;
import com.kaaction.modules.test.service.MmDadService;

/**
 * testController
 * @author test
 * @version 2017-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/test/mmDad")
public class MmDadController extends BaseController {

	@Autowired
	private MmDadService mmDadService;
	
	@ModelAttribute
	public MmDad get(@RequestParam(required=false) String id) {
		MmDad entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mmDadService.get(id);
		}
		if (entity == null){
			entity = new MmDad();
		}
		return entity;
	}
	
	@RequiresPermissions("test:mmDad:view")
	@RequestMapping(value = {"list", ""})
	public String list(MmDad mmDad, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MmDad> page = mmDadService.findPage(new Page<MmDad>(request, response), mmDad); 
		model.addAttribute("page", page);
		return "modules/test/mmDadList";
	}

	@RequiresPermissions("test:mmDad:view")
	@RequestMapping(value = "form")
	public String form(MmDad mmDad, Model model) {
		model.addAttribute("mmDad", mmDad);
		return "modules/test/mmDadForm";
	}

	@RequiresPermissions("test:mmDad:edit")
	@RequestMapping(value = "save")
	public String save(MmDad mmDad, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mmDad)){
			return form(mmDad, model);
		}
		mmDadService.save(mmDad);
		addMessage(redirectAttributes, "保存test成功");
		return "redirect:"+Global.getAdminPath()+"/test/mmDad/?repage";
	}
	
	@RequiresPermissions("test:mmDad:edit")
	@RequestMapping(value = "delete")
	public String delete(MmDad mmDad, RedirectAttributes redirectAttributes) {
		mmDadService.delete(mmDad);
		addMessage(redirectAttributes, "删除test成功");
		return "redirect:"+Global.getAdminPath()+"/test/mmDad/?repage";
	}

}